home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / capture < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  4.1 KB  |  132 lines

  1. #!/bin/ksh
  2. # @(#) capture.ksh 1.3 95/03/01
  3. # syntax: capture [ outfile ]
  4. # capture: captures current XENIX/UNIX console screen in a file
  5. # 91/01/18 john h. dubois iii (john@armory.com)
  6. # 92/03/17 Added '-' as special filename, and -t option.
  7. # 92/09/27 Use awk instead of head, print, etc.; delete trailing spaces;
  8. #          made filename be the default option;
  9. #          trap INT & QUIT to reset echo mode
  10. # 94/03/05 Make sure output file can be written before sending media copy seq.
  11. # 95/02/01 Added [naw] options.
  12. # 95/03/01 Fixed so '-' works as filename again.
  13.  
  14. unset ENV tty
  15.  
  16. name=${0##*/}
  17. Usage="Usage: $name [-ahn] [-t<tty>] [-w<width>] [filename]"
  18. typeset -i DelWhite=1 Width=0
  19. WrapOff=false
  20.  
  21. while getopts :ahnt:w: opt; do
  22.     case $opt in
  23.     h)
  24.     echo \
  25. "$name: capture a XENIX/UNIX console screen into a file.
  26. $Usage
  27. filename is the name of the file to capture the screen into.  If no filename is
  28. given, the screen will be captured into a file called \"screen\" in the current
  29. directory.  A filename of \"-\" will cause output to go to the standard output.
  30. A filename of \"-\" should only be used if the output of $name is redirected or
  31. if a tty other than the current tty is given with -t.
  32. Options:
  33. -h: Print this help.
  34. -n: Normally, $name removes trailing whitespace from the lines read from the
  35.     screen, because the lines are as wide as the screen and so would cause the
  36.     console to do a line wrap after every line, making it appear to be double-
  37.     spaced.  If -n is given, trailing whitespace is not removed.  This can be
  38.     used in cases where it is important that the lines in the file have a
  39.     uniform width.
  40. -w<width>: Truncate the output to <width> columns.  This can be used to e.g.
  41.     remove the rightmost column of a display that includes a border, so that it
  42.     can be displayed on the console without wrapping.  To do that, give a width
  43.     one less than the screen width, for example, -w79.
  44. -a: Add ANSI escape sequence to the output to turn off autowrap before the
  45.     captured display and turn it back on after the captured display.  This
  46.     allows full-width screen captures to be displayed without wrapping.  It
  47.     will work on the console under SCO 3.2v4 and on terminals that implement
  48.     the full set of ANSI X3.64-1979 screen attribute control sequences.
  49. -t<tty>: Specify the tty name of the screen to capture.  The default is to
  50.     capture the current screen.  If a screen other than the current screen is
  51.     to be captured, it must have no other processes reading from it, or $name
  52.     will compete for input with the other processes.  This generally means that
  53.     it should be disabled."
  54.     exit 0
  55.     ;;
  56.     t)  [[ "$OPTARG" = */* ]] && tty=$OPTARG || tty=/dev/$OPTARG
  57.     ;;
  58.     a)
  59.     WrapOff=true;;
  60.     w)
  61.     Width=$OPTARG || exit 1;;
  62.     n)
  63.     DelWhite=0;;
  64.     +?)
  65.     print -u2 "$name: options should not be preceded by a '+'."
  66.     exit 1
  67.     ;;
  68.     :) 
  69.     print -u2 "$name: Option '$OPTARG' requires a value.  Use -h for help."
  70.     exit 1
  71.     ;;
  72.     ?) 
  73.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  74.     exit 1
  75.     ;;
  76.     esac
  77. done
  78.  
  79. # remove args that were options
  80. let OPTIND=OPTIND-1
  81. shift $OPTIND
  82.  
  83. case $# in
  84. 0) outfile=screen;;
  85. 1) outfile=$1;;
  86. *) print -u2 \
  87. "$name: Too many arguments.
  88. $Usage
  89. Usage -h for help."
  90.    exit 1;;
  91. esac
  92.  
  93. set -e
  94.  
  95. trap "stty echo; exit" INT QUIT
  96.  
  97. # fd 0: tty being captured, for doing stty and reading media copy output.
  98. # fd 1: output file.
  99. # fd 2: tty being captured, for writing escape sequences.
  100.  
  101. [ -n "$tty" ] && exec < $tty 2> $tty
  102.  
  103. # Make sure we can write the output file before echoing media copy sequence
  104. if [ "$outfile" != - ]; then
  105.     exec > $outfile
  106. fi
  107.  
  108. stty -echo
  109.  
  110. $WrapOff && print -u2 -n "\033[?7l"    # turn off autowrap
  111. print -u2 -n "\033[2i"            # echo 'media copy' sequence
  112.  
  113. # Save output and then write it in case capture output goes to the screen
  114. # being read for some silly reason
  115. awk -v DelWhite=$DelWhite -v Width=$Width '
  116. {
  117.     if (DelWhite)
  118.     sub(" +$","")
  119.     if (Width > 0)
  120.     Lines[++i] = substr($0,1,Width)
  121.     else
  122.     Lines[++i] = $0
  123. }
  124. NR == 25 {
  125.     for (i = 1; i <= 25; i++)
  126.     print Lines[i]
  127.     exit
  128. }
  129. '
  130. $WrapOff && print -u2 -n "\033[?7h"    # turn on autowrap
  131. stty echo
  132.